package com.skz.job.service;

import com.skz.job.domain.Job;
import com.skz.job.repository.JobRepository;
import com.skz.job.spider.BossZhiPinSpider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import us.codecraft.webmagic.Spider;

import java.util.Date;
import java.util.List;

/**
 * @author: 宋开宗
 * @create: 2019-02-23 13:02
 **/
@Service
public class JobService {

    @Autowired
    private JobRepository jobRepository;
    private String listUrl = "https://www.zhipin.com/c101120100-p100101/?period=5&page=";
    private int startPage = 1;

    public void save() {
        //代理ip 免费很多不能用,暂时弃用
//        HttpClientDownloader httpClientDownloader = new HttpClientDownloader();
//        List<Proxy> proxies = new ArrayList<>();
//        ProxyIpSpider proxyIpSpider = new ProxyIpSpider();
//        Spider.create(proxyIpSpider).addUrl("https://www.kuaidaili.com/ops/").thread(1).run();
//        System.out.println("fetch proxy ip finish!");
//        for (int i = 0; i < proxyIpSpider.getIps().size(); i++) {
//            proxies.add(new Proxy(proxyIpSpider.getIps().get(i), Integer.valueOf(proxyIpSpider.getPorts().get(i))));
//        }
//        httpClientDownloader.setProxyProvider(new SimpleProxyProvider(proxies));
        Spider.create(new BossZhiPinSpider(listUrl, startPage)).addUrl(listUrl + startPage).addPipeline((resultItems, task) -> {
            Object job = resultItems.get("job");
            if (job != null) {
                Job bossJob = (Job) job;
                jobRepository.save(bossJob);
            }
        }).thread(1).run();
    }

    public List<Job> findTodayJobs() {
        return jobRepository.findByCreateDate(new Date());
    }

    public void del(Job job) {
        jobRepository.delete(job);
    }
}
